#include "StdAfx.h"
#include "GameMap.h"

enum DefaultValue
{
	MAP_FILE_FLAG	= MAKELONG(MAKEWORD('A','S'),MAKEWORD('M','P')),
};

CGameMap::CGameMap(void)
{
	m_MapSize.SetSize(0, 0);
	m_lGridSize = CGameMapGrid::DEFAULT_GRID_SIZE;
	m_pData = NULL;
}

CGameMap::~CGameMap(void)
{
	if (NULL != m_pData)
	{
		delete[] m_pData;
	}
}
void CGameMap::NewMap(CSize csMap, long lGridSize)
{
	m_MapSize = csMap;
	m_lGridSize = lGridSize;
	m_pData = new unsigned char[m_MapSize.cx * m_MapSize.cy];
	memset(m_pData, MAP_GRID_ROAD, m_MapSize.cx * m_MapSize.cy);
}
void CGameMap::LoadMap(CArchive& ar)
{
// 	long flag;
// 	ar >> flag;
// 	if (MAP_FILE_FLAG == flag)
// 	{
// 		ar >> m_MapSize;
// 		m_pData = new unsigned char[m_MapSize.cx * m_MapSize.cy];
// 		if (NULL != m_pData)
// 		{
// 			ar.Read(m_pData, m_MapSize.cx * m_MapSize.cy);
// 			TRACE(_T("map loa over!\n"));
// 		}
// 	}
	m_MapSize.SetSize(2048, 1000);
	m_pData = new unsigned char[m_MapSize.cx * m_MapSize.cy];
	if (NULL != m_pData)
	{
		ar.Read(m_pData, m_MapSize.cx * m_MapSize.cy);
		TRACE(_T("map loa over!\n"));
	}
}
void CGameMap::SaveMap(CArchive& ar)
{
	// ar << MAP_FILE_FLAG << width_ << height_;
}
void CGameMap::OnGridClicked(CGameMapGrid::CGridPos& pos, CGameMapGrid::GridType type)
{
	int loc = pos_2_location(pos);
	if (CGameMapGrid::GRID_TYPE_ENTRY == type || CGameMapGrid::GRID_TYPE_EXPORT == type)
	{
		return;
	}
	m_pData[loc] = type;
	m_GridStore[loc] = type;
}